package com.jumi.microservice.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.core.web.page.TableDataInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.reponse.AddressResponse;
import com.jumi.microservice.dto.reponse.JmUserReponse;
import com.jumi.microservice.dto.request.AddressQueryRequest;
import com.jumi.microservice.entity.JmAddress;
import com.jumi.microservice.service.IAddressService;
import com.jumi.microservice.service.IJmAddressService;
import com.jumi.microservice.service.IJmAreaService;
import com.jumi.microservice.service.PageService;
import com.jumi.microservice.util.WrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 买家地址信息表 前端控制器
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-09-08
 */
@DubboService
@RestController
@RequestMapping("/jm-address")
@Api(value = "地址管理", tags = "地址管理")
public class JmAddressController implements IAddressService {

    @Autowired
    IJmAreaService areaService;

    @Autowired
    private IJmAddressService jmAddressService;


    @PostMapping("/getPage")
    @ApiOperation("查询地址信息分页")
    public TableDataInfo<JmUserReponse> getPage(AddressQueryRequest queryRequest,
                                                Integer size, Integer current) {
        Page page = new Page(current, size);
        QueryWrapper wrapper = WrapperUtil.getWrapper(null, queryRequest);
        page = jmAddressService.getPage(page, wrapper);
        return PageService.getDataTableInfo(page);
    }


    @GetMapping("/addressListByUserId/{userId}")
    @ApiOperation("根据用户id获取收货地址")
    public ResponseResult addressListByUserId(@PathVariable Integer userId) {

        List<JmAddress> addresses =
                jmAddressService.list(new QueryWrapper<JmAddress>().eq("uid", userId));
        return ResponseResult.success(addresses);
    }

    @PostMapping("/getArea")
    @ApiOperation(value = "查询地区信息")
    public ResponseResult getArea(@ApiParam(value = "父id", required = true) @RequestParam Integer parentId) {
        if (ObjectUtil.isEmpty(parentId)) {
            return ResponseResult.error("parentId不能为空!");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("area_parent_id", parentId);
        //wrapper.eq("area_deep",deep);
        List list = areaService.list(wrapper);
        return ResponseResult.success(list);
    }

    @Override
    public AddressResponse getAddressById(Integer id) {
        JmAddress byId = jmAddressService.getById(id);
        AddressResponse response = null;
        if (ObjectUtil.isNotEmpty(byId)) {
            response = new AddressResponse();
            BeanUtils.copyProperties(byId, response);
        }
        return response;
    }

    @Override
    @GetMapping("/getDefaultAddressByUid")
    public AddressResponse getDefaultAddressByUid(Integer uid) {
        QueryWrapper<JmAddress> jmAddressQw = new QueryWrapper<>();
        jmAddressQw.eq("uid", uid);
        jmAddressQw.eq("is_default", 1);
        JmAddress jmAddress = jmAddressService.getOne(jmAddressQw);
        AddressResponse response = new AddressResponse();
        if (jmAddress == null) {
            jmAddress=jmAddressService.findMaxNotDefaultObj(uid);
            if(jmAddress ==null)
                return null;
            else{
                BeanUtils.copyProperties(jmAddress, response);
                return response;
            }
        }
        BeanUtils.copyProperties(jmAddress, response);
        return response;
    }
}
